const Model = require('./index')

class VisitModel extends Model {
  /**
   * 访问记录分页列表查询
   * @param {*} pageNo
   * @param {*} pageSize
   * @param {*} param2
   * @returns
   */
  async findList (pageNo, pageSize, { andConditions, betweenConditions }) {
    const completeSql = this.generateBetweenClause(betweenConditions, this.generateWhereClause(andConditions))
    const sql = `select * from visit ${completeSql}
      order by date desc
      limit ${pageSize} offset ${(pageNo - 1) * pageSize}`
    try {
      const list = await this.db.query(sql)
      const total = await this.total(completeSql)
      return {
        list,
        total
      }
    } catch (error) {
      throw new Error(error)
    }
  }

  async total (conditionSql) {
    const sql = `select count(*) as total from visit ${conditionSql}`
    try {
      const data = await this.db.query(sql)
      return data[0].total
    } catch (error) {
      throw new Error(error)
    }
  }
}

module.exports = new VisitModel('visit')
